package com.example.patentback.service.impl;

import com.example.patentback.dao.PatentMapper;
import com.example.patentback.entity.Patent;
import com.example.patentback.entity.PatentDocumentDto;
import com.example.patentback.service.PatentService;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class PatentServiceImpl implements PatentService {

    @Autowired
    PatentMapper patentMapper;

    @Override
    public List<Patent> selectSixPatents(String name, int start) {
        int pageSize = 6;
        int index = (start - 1) * pageSize;
        RowBounds rowBounds = new RowBounds(index, pageSize);
        List<Patent> patents = new ArrayList<>();
        if (name.length() == 0) {
            patents = patentMapper.selectByRowBounds(new Patent(), rowBounds);
        } else {
            Example example = new Example(Patent.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.orLike("name", "%" + name + "%");
            patents = patentMapper.selectByExampleAndRowBounds(example, rowBounds);
        }
        return patents;
    }

    @Override
    public Patent insertPatent(PatentDocumentDto patentDocumentDto, Long documentId) {
        Patent patent = new Patent();
        patent.setName(patentDocumentDto.getName());
        patent.setType(patentDocumentDto.getType());
        patent.setHelperId(patentDocumentDto.getHelperId());
        patent.setContent(patentDocumentDto.getContent());
        patent.setDocumentId(documentId);
        patent.setCreateTime(new Date());
        patent.setStatus(0);
        patentMapper.insertUseGeneratedKeys(patent);
        return patent;
    }

    @Override
    public List<Patent> selectAllPatents(List<Long> patentIds, String name, int current, int size,int status1,int status2) {
        int pageSize = size;
        int index = (current - 1) * pageSize;
        RowBounds rowBounds = new RowBounds(index, pageSize);
        List<Patent> patents = new ArrayList<>();
        Example example = new Example(Patent.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", patentIds);
        if(status1==5&&status2==6){
            ArrayList<Integer> status = new ArrayList<>();
            status.add(status1);
            status.add(status2);
            criteria.andIn("status",status);
        }
        if (name == null || name.length() == 0) {
            patents = patentMapper.selectByExampleAndRowBounds(example, rowBounds);
        } else {
            criteria.andLike("name", "%" + name + "%");
            patents = patentMapper.selectByExampleAndRowBounds(example, rowBounds);
        }
        return patents;
    }

    @Override
    public int updateStatus(Long id, int sta) {
        Patent patent = new Patent();
        patent.setId(id);
        patent.setStatus(sta);
        return patentMapper.updateByPrimaryKeySelective(patent);
    }

    @Override
    public Patent selectPatent(Long id) {
        System.out.println(id);
        Example example = new Example(Patent.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);
        return patentMapper.selectByExample(example).get(0);
    }

    @Override
    public int deletePatents(List<Long> ids) {
        ids.forEach(id -> {
            Example example = new Example(Patent.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("id", id);
             patentMapper.deleteByExample(example);
        });
        return ids.size();
    }

}
